<?php
/**
 * @description : oauth databases config setting.
 * @class-name  : OAuthDBOperateConfig()
 * @author      : stephen.mo
 * @date        : Jul 22,2010 
 */
class OAuthModelConfig {/*{{{*/

    #expire time with token.(OAuth Service Token Expiry Time)
    const EXPIRE_TIME                   = 10;                   //unit:min 

    #memcache
    const MEMCACHE_NODE_NAME            = 'memcached';          //xml node name.
    const MEMCACHE_SUB_NODE_NAME        = 'hosts';              //xml sub-node name.
    const OPEN_CLOSE_MEMCACHED          = true;                 //is or not using memcache.
    const MEMCACHE_EXPIRED_TIME         = 5;                    //memcache value expiry time.(APIs value on memcache server).
    const SAVE_MEMBER_INFO_ETIME        = 43200;                //1 month (30d * 24h * 60m)
    const USING_MEM_SAVE_MEMBER         = true;                 //if using memcache to save members' info ,will check it from memcache.

    #database & table setting.
    const DATABASE_CONFIG_PATH          = '/data/OAuth/conf/server_db_list.xml';
    const MEMCACHE_CONFIG_XML           = '/data/OAuth/conf/server_db_list.xml';
    const THIRD_PARTIES_LIST            = '/data/OAuth/conf/third_parties_list.xml';
    const SUPPORT_SHOP_LIST             = '/data/OAuth/conf/support_shop_list.xml';
    const TVB_AUTH_KEY_XML              = '/data/OAuth/conf/tvb_auth_key.xml';
    const DEFAULT_SRC_PATH              = '/data/httpd/htdocs/';

    #table name setting.
    const TABLE_NAME_OF_CONSUMER        = 'oauth_consumer_list';
    const TABLE_NAME_OF_NONCE           = 'oauth_nonce_list';
    const TABLE_NAME_OF_GENERIC         = 'oauth_generic_vars';
    const TABLE_NAME_OF_MS_CONSUMER     = 'oauth_ms_consumer';
    
    #define the free type of join plan and after register member.
    const JOIN_AFTER_REG                = '076ec89a56941a27a313f07664597c5e';
    const OAUTH_MOBILE_FLAG             = 'mobile';

    #define the third-parties login parameters.
    const PCCW_OAUTH_FROM_TYPE          = 'pccw';                  //the third-party for MembershipAPi
    const PCCW_OAUTH_FROM_DEVICE        = 'mobile_phone';          //ask MembershipAPi,the source from mobile.
    const OAUTH_DEBUG_LOG_PATH          = '/data/OAuth/log/';      //log path
    const OAUTH_PCCW_JAR_PATH           = '/data/OAuth/jars/';     //decrypt string's java jar package.
    const OAUTH_NEED_CHKTIMESTAMP       = true;                    //check the timestamp of decrypt string.
    const OAUTH_PCCW_TIMESTAMP_EXPIRY   = 60;                      //allow expiry time.unit is min
    const OAUTH_OPEN_THIRD_PARTY_LOGIN  = true;                    //Is or not using Third-party login function.

    #define the third payment (telco) vars.
    const OAUTH_THIRD_PAYMENT_CONF_PATH = '/data/OAuth/conf/payment/';  //third-party payment sub path.<include tvb.com>
    const OAUTH_PUBLIC_TELCO_PAYMENT    = '/m/telcopayment.php';        //third-party payment public submit file.<except tvb.com>
    const OAUTH_USING_NETWORK_TYPE_WIFI = 'WIFI';                       //wifi network.
    const OAUTH_USING_NETWORK_TYPE_3G   = '3G';                         //3G??
    const OAUTH_USING_NETWORK_TYPE_2G   = '2G';                         //2G??
    const AREA_TELPHONE_OF_HONGKONG     = '852';                        //hongkong's telphone area number.
    const GET_H3G_MSISDN_EXPIRE         = 300;                          //uint is seconds.
    const THIRD_PAYMENT_CRUMB_KEY       = 'tvb_third_payment';          //crumb key
    const THIRD_PAYMENT_EXPIRE_TIME     = 300;                          //unit is seconds.
    const MOBILE_LOGIN_INFO_EXPIRE_TIME = 300;                          //mobile logined information expire time.  unit is seconds. 
    const DEFAULT_GATEWAY_OF_H3G        = 'h3g';
    const DEFAULT_GATEWAY_OF_CSL        = 'csl';
    const DEFAULT_GATEWAY_OF_PCCW       = 'pccw';
    const DEFAULT_GATEWAY_OF_SMARTONE   = 'smartone';


    #package type
    const PKG_TYPE_SP                   = 0; //single payment
    const PKG_TYPE_SF                   = 1; //single free
    const PKG_TYPE_CFP                  = 2; //complex first payment
    const PKG_TYPE_CLP                  = 3; //complex last payment
    const PKG_TYPE_DEFAULT              = 0; //single payment.

    #allow header execute match regular.
    const NEED_MATCH_REGULAR            = false; //a switch of regular domain.default off.
    
    #is need to check oauth token.
    const IS_NEED_CHECK_OAUTHTOKEN      = false;

    #control is or not using real env for mobile pages.
    const DEFAULT_DEBUG_SWITCH          = false;
    const USING_TEMPLATE_ON_REAL_ENV    = true;
    const DEFAULT_MOBILE_TEMPLATE       = '/m_template/';
    const DEFAULT_CONST_SUCCESS         = 'success';
    const DEFAULT_CONST_FALIURE         = 'failure';
    const DEFAULT_CONST_FINISH          = 'finish';
    const DEFAULT_CONST_EXISTS          = 'exists';

    #define the page vars.
    const M_PAGE_OF_LOGIN               = 'login';
    const M_PAGE_OF_CMODEL              = 'cmodel';
    const M_PAGE_OF_FPASSWORD           = 'fpassword';
    const M_PAGE_OF_CPLAN               = 'cplans';
    const M_PAGE_OF_THIRDP_LOGIN        = 'thirdlogin';
    const M_PAGE_OF_REGISTRATION        = 'register';
    const M_PAGE_OF_INFORMATION         = 'information';
    const M_PAGE_OF_PCONFIRM            = 'pconfirm';
    const M_PAGE_OF_PAYMENT             = 'payment';
    const M_PAGE_OF_FREE_PAYMENT        = 'free_payment';
    const M_PAGE_OF_MESSAGE             = 'message';
    const M_PAGE_OF_PLAN_SIGNIN         = 'qlogin';
    const M_PAGE_OF_PLAN_DETAILS        = 'pdetails';
    const M_PAGE_OF_PLAN_REQUEST        = 'requests';
    const M_PAGE_OF_PLAN_JOINS          = 'joins';
    const M_PAGE_OF_ERROR               = 'error';
    const M_PAGE_OF_PROMPT              = 'prompt';
    const M_PAGE_OF_LOGIN_IPTV          = 'login_iptv';

    #email's format.
    const DEFAULT_EMAIL_PREG_MATCH      = '/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/';

    #payment return url define.
    static public $payment_successUrl  = '/m/plan_transaction_succ.php';
    static public $payment_failUrl     = '/m/plan_transaction_fail.php';
    static public $payment_errorUrl    = '/m/plan_transaction_error.php';

    #public telcopayment callback path define.
    static public $telco_payment_succ  = 'success.php';
    static public $telco_payment_fail  = 'failure.php';
    static public $telco_payment_proc  = 'process.php';
    
    #mapping the third-parties language setting.
    static public $mapping_third_parties_lang = array(/*{{{*/
        'en-hk'=>'eng',
        'zh-hant-hk'=>'chi',
        'zh-hans-hk'=>'chi',
        'chi'=>'zh-hant-hk',
        'eng'=>'en-hk',
    );/*}}}*/

    #define the temploate page name.
    static public $mobile_teplate_page = array(/*{{{*/
        'login'       =>'login.html',
        'cmodel'      =>'choose_model.html',
        'fpassword'   =>'forget_password.html',
        'cplans'      =>'choose_plan.html',
        'thirdlogin'  =>'third_parties_login.html',
        'register'    =>'register.html',
        'information' =>'information.html',
        'pconfirm'    =>'plan_confirm.html',
        'payment'     =>'pay.html',
        'free_payment'=>'free_plans.html',
        'message'     =>'message.html',
        'qlogin'      =>'quick_login.html',
        'pdetails'    =>'plan_detail.html',
        'requests'    =>'third_plan_index.html',
        'joins'       =>'information.html',
        'login_iptv'  =>'login_iptv.html',

    );/*}}}*/
    
    #define check page language list for all mobile payment pages with ajax action
    static public $need_chk_plang = array(/*{{{*/
        'orderRef',
        'orderRef_special',
        'orderRefFree',
        'orderRefFree_special'
    );/*}}}*/

    #define check oauth token list for all mobile payment pages with ajax action
    static public $allow_ajax_actions = array(/*{{{*/
        'memberReg',
        'joinUserInfo',
        'orderRef_special',
        'orderRefFree_special',
        'forgetPassword',
        'quickSignIn',
        'quickRegister',
        'tmpSignIn',
        'tmpRegister',
        'logout',

    );/*}}}*/

    #define the third-parties support network-types.
    static public $support_network_type = array(/*{{{*/
        'h3g'=>array('WIFI','3G','2G'),
        'csl'=>array(),

    );/*}}}*/

    #allow header file redirect domain.
    static public $default_allow_domain = array(/*{{{*/
        'member.tvb.com',
        'm.tvb.com',
        'msapi.tvb.com',
        'www.consumer.com',
    );/*}}}*/

    #mobile third-parties payment supply conf suffix.
    static $supply_suffix_name = array(/*{{{*/
        'xml',

    );/*}}}*/

    #shop ids for TVB.COM package.
    static $shop_for_TVB = array(/*{{{*/
        
        
       1,3,5 
    );/*}}}*/

    #payment real lang for paypal. 
    static public $langArr = array(/*{{{*/
            'zh-hant-hk' => 'C',
            'zh-hans-hk' => 'X',
            'en-hk'      => 'E',
    );/*}}}*/
    
    #payment real current code for paypal.
    static public $currencyArr = array(/*{{{*/
            'HKD' => '344',
            'USD' => '840',
            'SGD' => '702',
            'CNY' => '156',
            'RMB' => '156',
            'TWD' => '901',
            'JPY' => '392',
            'AUD' => '036',
            'EUR' => '978',
            'CAD' => '124',
        );/*}}}*/

    /**
     * @method-name : get support shop id for tvb package list.
     * @parameter   : (string)$model - shop 's model  
     * @return      : array
     * @author      : stephen.mo <stephen.mo@achievo.com>
     * @date        : Jan 5,2011
     */
    static public function getShopIdByModel($model="TVB") {/*{{{*/
        $ret = array();
        $file_path = OAuthModelConfig::SUPPORT_SHOP_LIST;
        if (is_file($file_path)) {
            $config = ConfigReaderFactory::getInstance()->getConfigReader($file_path)->getConfig();
            if (empty($config)) return $ret;
            foreach($config->shop_id->item as $root) {
                $ret[] = (int)$root;
            }
        }
        return $ret;

    }/*}}}*/


    /**
     * @method-name : getMicroFloatTime().
     * @parameter   : NULL
     * @return : string.
     * @author : stephen
     * @date : 2010/08/30
     */
    static public function getMicroFloatTime() {/*{{{*/
        list($usec, $sec) = explode(" ", microtime());
        return $usec.$sec;
    }/*}}}*/
    
    /**
     * @method-name : getErrorInfo().
     * @parameter   : (int)$state - the retFlag.
     * @parameter   : (int)$errorCode - return error code.
     * @parameter   : (string)$errorMsg - Specially error message.
     * @return : json string.
     * @author : stephen
     * @date : 2010/08/30
     */
    static public function getErrorInfo($state=0,$errorCode,$errorMsg=NULL) {/*{{{*/
        $array['retFlag'] = $state;
        $array['errCode'] = $errorCode;
        $array['info'] = (empty($errorMsg)) ? self::$errorCodeTxt[$errorCode] : $errorMsg;
        return json_encode($array);
    }/*}}}*/
    
    /**
     * @method-name : getAjaxErrInfo().
     * @parameter   : 
     * @parameter   :
     * @parameter   :
     * @return : json string.
     * @author : stephen
     * @date : 2010/08/31
     */
    static public function getAjaxErrInfo($state=1,$errCode=1,$lang=NULL,$info=NULL,$retKey=NULL,$retVal=NULL) {/*{{{*/
        $lang           = (empty($lang)) ? 'zh-hant-hk' : $lang; 
        $ret['retFlag'] = (!empty($state)) ? $state : 0;
        $ret['errCode'] = (!empty($errCode)) ? $errCode : 1000;
        $ret['info']    = (!empty($info)) ? $info : '_submited';
        $errorCodeTxt   = OPI::$errorCodeTxt;

        if ($state==1) {
            $data_key = (!empty($retKey)) ? $retKey : 'order_ref';
            $ret['data'] = array($data_key=>$retVal);
        }
        else {
            $retVal_ = (empty($retVal)) ? $errorCodeTxt[$lang][$errCode] : $retVal;
            $error_info = array($retKey=>$retVal_);
            $ret['errors'] = $error_info;
        }
        return json_encode($ret);
    }/*}}}*/
    
    /**
     * @method-name : getAjaxReturnInfo().
     * @parameter   : (int)$state ; state of the info.
     * @parameter   : (init)$errCode 
     * @parameter   : (string)$lang
     * @parameter   : (string)$info
     * @parameter   : (array)$data.
     * @parameter   : (bool)$flag - Is special replace string?
     * @return      : jsong string.
     * @author      : stephen.mo <stephen.mo@achievo.com>
     * @date        : Dec 7,2010
     */
    static public function getAjaxReturnInfo($state=1,$errCode=1,$lang=NULL,$info=NULL,$data=array(),$flag=false) {/*{{{*/
        $lang           = (empty($lang)) ? 'zh-hant-hk' : $lang; 
        $ret['retFlag'] = (!empty($state)) ? $state : 0;
        $ret['errCode'] = (!empty($errCode)) ? $errCode : 1000;
        $ret['info']    = (!empty($info)) ? $info : '_submited';
        $errorCodeTxt   = OPI::$errorCodeTxt[$lang];       

        //output messages.
        if ($flag) {
            $rep_string   = $data['rep'];
            $local_string = (isset($data['sou']) && !empty($data['sou'])) ? $data['sou'] : $errorCodeTxt[$errCode];
            $ret['data']  = array('msgTxt'=>$local_string);
            if (isset($data['tag']) && !empty($data['tag'])) {
                $tmp_string   = str_replace($data['tag'],$data['rep'],$local_string);
                $ret['data'] = array('msgTxt'=>$tmp_string);
            }
        }
        else {
            if ($state) {
                $tmp_data = array();
                foreach((array)$data as $key=>$val) {
                    $tmp_data[$key]=$val; 
                }
                $ret['data'] = $tmp_data;
            }
            else {
                $ret['errors'] = array('errTxt'=>$errorCodeTxt[$errCode]);
            }
        }
        
        return json_encode($ret);
    }/*}}}*/

    /**
     * @method-name : getRealUnit().
     * @parameter   : (string)$unit  - The unit char.(d,w,m,y)
     * @parameter   : (int)$duration - How long with the plan service support?
     * @parameter   : (string)$lang  - User's language.
     * @description : return a plan package's text with unit and duration.
     * @return : string.
     * @author : stephen.mo
     * @date : 2010/09/02
     */
    static public function getRealUnit($unit,$duration,$lang,$free_period=array()) {/*{{{*/
        $lang = (empty($lang)) ? OAuthConfig::DEFAULT_SUPPORT_LANG : $lang;
        $unit               = strtolower($unit);
        $duration           = intval($duration);
        $unitArr            = OPI::$choose_plan_package_unit;
        $choose_plans       = OPI::$choose_plan_info;
        $periodOfServiceArr = OPI::$period_of_service;
        $period_of_service  = $periodOfServiceArr[$lang];
        
        //set chars by unit & period unit.
        $real_unit = (isset($unitArr[$lang][$unit])) ? $unitArr[$lang][$unit] : $unit;
        $ret = NULL;
        
        if($duration>1) {//plural
            $real_unit = ($lang=='en-hk') ? $real_unit.'s' : $real_unit;
        }
        $real_unit = $duration.' '.$real_unit;

        //count free period.
        $free_period_unit = NULL;
        if (!empty($free_period) && $free_period[1]!=0) {
            $free_duration = $free_period[1];
            $freePeriodArr = OPI::$choose_plan_package_free_period;
            $free_time     = $freePeriodArr[$lang];
            $free_unit     = $free_period[2];
            $_real_unit    = (isset($unitArr[$lang][$free_unit])) ? $unitArr[$lang][$free_unit] : $unit;
            if ($free_duration>1) {
                $_real_unit  = ($lang=='en-hk') ? $_real_unit.'s' : $_real_unit;
            }
            $free_period_unit = '+'.$free_time.$free_duration.$_real_unit;
        }
        $result = $period_of_service;
        $result .= (!empty($free_period_unit)) ? $real_unit.'<em>'.$free_period_unit.$choose_plans[$lang]['cplan_service'].'</em>' : $real_unit;
        return $result;

    } /*}}}*/
    
    /**
     * @method-name : getFreePeriodPlan().
     * @parameter   : (array)$free_period - the free period info.
     * @parameter   : (strint)$lang - language.
     * @parameter   : (int)$duration - real time for package plan.
     * @parameter   : (string)$pkg_unit - the unit of package plan.
     * @parameter   : (int)$price - how much with this package plan.
     * @parameter   : (string)$dollar - the pkg's dollar char.
     * @description : get the plan's free period infos.
     * @return : string.
     * @author : stephen.mo
     * @date : 2010/09/02
     */
    static public function getFreePeriodPlan($free_period,$lang,$duration=0,$pkg_unit='m',$price=0,$dollar=NULL) {/*{{{*/
        $lang = (empty($lang)) ? OAuthConfig::DEFAULT_SUPPORT_LANG : $lang;
        if (empty($free_period)) return NULL;
        $free_advance  = $free_period[0];//is or not free.
        $free_duration = $free_period[1];//how long with free time.
        $free_unit     = strtolower($free_period[2]);//free time's unit.
        $pkg_unit      = strtolower($pkg_unit);

        if ($free_duration==0) return NULL;
        if ($duration==0) return NULL;
        if ($price==0) return NULL;
        if ($pkg_unit!='y' && $pkg_unit!='m') return NULL;
        $unitArr       = OPI::$choose_plan_package_unit;
        $freePeriodArr = OPI::$choose_plan_package_free_period;
        $cplan_pages   = OPI::$choose_plan_info;

        //min unit is month.
        $base_unit  = ($pkg_unit=='y') ? ($duration*12) : ($pkg_unit=='m' ? $duration : 0);
        $add_unit   = ($free_unit=='y') ? ($free_duration*12) : ($free_unit=='m' ? $free_duration : 0);
        $aver_price = ($price/($base_unit+$add_unit)).'**';
        $result = '('.$cplan_pages[$lang]['cplan_aver'].$dollar.$aver_price.')'; 

        return $result;
    }/*}}}*/

    /**
     * @method-name : setDefaultLang().
     * @parameter   : (string)$lang  - the current language.
     * @description : setting the default language
     * @return : array
     * @author : stephen.mo
     * @date : 2010/09/06
     */
    static public function setDefaultLang($lang) {/*{{{*/
        $default = array('zh-hant-hk'=>'','zh-hans-hk'=>'','en-hk'=>'');
        foreach($default as $key=>$val) {
            if ($key==$lang) {
                $default[$key] = ' selected ';
                break;
            }
        }
        return $default;
    }/*}}}*/

}/*}}}*/

?>
